package cn.tedu.store.service.impl;

import cn.tedu.store.config.util.HttpUtil;
import cn.tedu.store.config.util.RedisService;
import cn.tedu.store.entity.HistClientList;
import cn.tedu.store.entity.PbkoXxk;
import cn.tedu.store.mapper.EmployeeMapper;
import cn.tedu.store.mapper.PbkoXxkMapper;
import cn.tedu.store.service.PbkoXxkService;
import cn.tedu.store.temperatureDetection.entity.dto.EmployeeDto;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class PbkoXxkServiceImpl extends ServiceImpl<PbkoXxkMapper,PbkoXxk> implements PbkoXxkService  {

    @Resource
    PbkoXxkMapper pbkoXxkMapper;

    @Resource
    EmployeeMapper employeeMapper;

    @Override
    public List<PbkoXxk> selcet(int page,int pageSize) {
//        LambdaQueryWrapper<PbkoXxk> pbkoXxkLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        pbkoXxkLambdaQueryWrapper.

        List<PbkoXxk> pbkoXxks =  pbkoXxkMapper.selcet(page,pageSize);
        pbkoXxks.stream().forEach(s->{
            EmployeeDto byEmplCode = employeeMapper.getByEmplCode(s.getEmpId());

            if (Objects.isNull(byEmplCode)) {

            }else {
            if (!StringUtils.isBlank(byEmplCode.getEmplType())) {
//                if (byEmplCode.getEmplType().equals("D")) {
//                    s.setEmplTpye("医生");
//                } else if (byEmplCode.getEmplType().equals("N")) {
//                    s.setEmplTpye("护士");
//                } else if (byEmplCode.getEmplType().equals("T")) {
//                    s.setEmplTpye("医技");
//                } else if (byEmplCode.getEmplType().equals("P")) {
//                    s.setEmplTpye("药剂");
//                } else if (byEmplCode.getEmplType().equals("O")) {
//                    s.setEmplTpye("其他");
//                } else if (byEmplCode.getEmplType().equals("F")) {
//                    s.setEmplTpye("财务");
//                }\
                System.out.println(byEmplCode);
                s.setEmplTpye(byEmplCode.getEmplType());
            }
            }
        });

        return pbkoXxks;
    }

    public static void main(String[] args) {
//        Date date = new Date();
//        long time = date.getTime();
//         long end =  time+60000*30;
//        Date date1 = new Date(end);
//        System.out.println(date);
        System.out.println(new Date(new Date().getTime()+60000*30));


    }

    @Override
    public void add() throws ParseException {
        HashMap<String, String> map = new HashMap<>();
        map.put("apikey","On1x4WL5OSaTDJVVJZJriVA8bvzPGc4N");
        JSONObject jsonObject1 = new JSONObject();
        String s1 = jsonObject1.toJSONString();

        JSONObject jsonObject = JSONObject.parseObject
                (HttpUtil.doPostJson("https://lvzhouapi.h3c.com/device/historyclients?devSN=219801A2X08222E00028&skipnum=0&limitnum=10", map, s1));
        JSONArray histClientList = jsonObject.getJSONArray("histClientList");
        List<HistClientList> histClientLists = histClientList.toJavaList(HistClientList.class);
        if (histClientList.size()>0){

            for (HistClientList mac : histClientLists) {
                SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                String format = simpleDateFormat1.format(mac.getOnLineDate());
                mac.setDate(format);
                HistClientList histClientList1= employeeMapper.get(mac);

                if (Objects.isNull(histClientList1)){

                    employeeMapper.addOneMac(mac);

                }
            }
        }

    }

    @Override
    public List<HistClientList> getMACList() {
        return employeeMapper.getListMac();

    }
    @Resource
    RedisService redisService;
    @Override
    public void preService(String phone, String mac) {
        //1 获取数据库mac 地址
        Boolean aBoolean = redisService.hasKey(mac);

        System.out.println(aBoolean);
        if (!aBoolean){
            System.out.println("判断是吧进入");
            redisService.setStringKeyAndValue(mac,phone);
        }
        //2 比对是否一致

        //2.1 不一致直接返回

        //2.2 一致发送语音+短信


    }
    @Autowired //StringRedisTemplate它是redisTemplate的子类，而且独有特点：它保存的数据key和value都是string
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void contrastMac() {
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        String format = simpleDateFormat1.format(date);
        List<HistClientList> listMac = employeeMapper.getToDayMac(format);
        for (HistClientList histClientList : listMac) {
            //手机格式mac  00:00:00:00:00:00
            String substring = histClientList.getClientMAC().replaceAll("-", "").replaceAll("(.{2})", ":$1").substring(1);
            if (redisService.hasKey(substring)){
                String s = stringRedisTemplate.opsForValue().get(substring);
                Boolean delete = stringRedisTemplate.delete(substring);
                if (delete){
                    //发送短信


                }
            }



        }

    }


}
